package com.jeesite.modules.common.service;

import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.common.dao.OldDataDao;
import com.jeesite.modules.common.entity.*;
import com.jeesite.modules.file.service.FileUploadService;
import com.jeesite.modules.task.action.dao.DcSupTaskActionDao;
import com.jeesite.modules.task.action.entity.DcSupTaskAction;
import com.jeesite.modules.task.audit.dao.DcSupTaskAuditDao;
import com.jeesite.modules.task.audit.entity.DcSupTaskAudit;
import com.jeesite.modules.task.deploy.dao.DcSupTaskInfoDao;
import com.jeesite.modules.task.deploy.entity.DcSupTaskInfo;
import com.jeesite.modules.task.deploy.entity.DcSupTaskInfoAdd;
import com.jeesite.modules.task.deploy.service.DcSupTaskInfoService;
import com.jeesite.modules.task.feedback.dao.DcSupTaskFeedbackDao;
import com.jeesite.modules.task.feedback.entity.DcSupTaskFeedback;
import com.jeesite.modules.task.feedback.service.DcSupTaskFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OldDataService {
    @Autowired
    private OldDataDao oldDataDao;

    @Autowired
    private DcSupTaskInfoService dcSupTaskInfoService;
    @Autowired
    private DcSupTaskInfoDao dcSupTaskInfoDao;
    @Autowired
    private DcSupTaskActionDao dcSupTaskActionDao;
    @Autowired
    private DcSupTaskFeedbackDao dcSupTaskFeedbackDao;
    @Resource
    private FileUploadService fileUploadService;
    @Autowired
    private DcSupTaskFeedbackService dcSupTaskFeedbackService;
    @Autowired
    private DcSupTaskAuditDao dcSupTaskAuditDao;


    @Transactional(rollbackFor = Exception.class)
    public String findlistTask() {
        String code="ok";
        int count=0;
        try {
            List<OtsDeployParent> otsDeployParentList = oldDataDao.findlistTask();
            for (OtsDeployParent otsDeployParent:otsDeployParentList) {
                DcSupTaskInfo dcSupTaskInfo=new DcSupTaskInfo();
                dcSupTaskInfo.setId(String.valueOf(otsDeployParent.getTaskid()));
                //任务年度
                dcSupTaskInfo.setTaskYear(String.valueOf(otsDeployParent.getYears()));
                //任务类型主键
                dcSupTaskInfo.setTaskTypeCode(String.valueOf(otsDeployParent.getSourceid()));
                //任务类型名称
                dcSupTaskInfo.setTaskTypeName(otsDeployParent.getSourceName());
                //任务标题
                dcSupTaskInfo.setTaskTitle(otsDeployParent.getTitle());
                //任务内容
                dcSupTaskInfo.setTaskContent(otsDeployParent.getTkname());
                //任务开始时间
                dcSupTaskInfo.setTaskStartTime(otsDeployParent.getStarttime());
                //任务结束时间
                dcSupTaskInfo.setTaskEndTime(otsDeployParent.getEndtime());
                //牵头领导
                //牵头单位
                OtsDeployMap otsDeployMap=new OtsDeployMap();
                otsDeployMap.setPtaskid(otsDeployParent.getTaskid());
                otsDeployMap=oldDataDao.findotsDeployMap(otsDeployMap);
                OtsSubject otsSubject=new OtsSubject();
                otsSubject.setTaskid(otsDeployMap.getTaskid());
                List<OtsSubject> otsSubjectList=oldDataDao.findListOtsSubject(otsSubject);
                StringBuffer taskLeader=new StringBuffer();
                StringBuffer taskLeaderName=new StringBuffer();
                StringBuffer taskLeaderOrg=new StringBuffer();
                StringBuffer taskLeaderOrgName=new StringBuffer();
                for (OtsSubject osj:otsSubjectList) {
                    //牵头领导主键
                    if(osj.getFlag()==3){
                        taskLeader.append(osj.getSubjectid());
                        taskLeader.append(",");
                    }
                    //牵头领导名称
                    if(osj.getFlag()==3){
                        taskLeaderName.append(osj.getSubjectname());
                        taskLeaderName.append(",");
                    }
                    //牵头单位主键
                    if(osj.getFlag()==0){
                        taskLeaderOrg.append(osj.getSubjectid());
                        taskLeaderOrg.append(",");
                    }
                    //牵头单位名称
                    if(osj.getFlag()==0){
                        taskLeaderOrgName.append(osj.getSubjectname());
                        taskLeaderOrgName.append(",");
                    }
                }
                //牵头领导
                if(StringUtils.isNotBlank(taskLeader)){
                    dcSupTaskInfo.setTaskLeader(taskLeader.toString().substring(0,taskLeader.toString().length()-1));
                }
                //牵头领导名称
                if(StringUtils.isNotBlank(taskLeaderName)){
                    dcSupTaskInfo.setTaskLeaderName(taskLeaderName.toString().substring(0,taskLeaderName.toString().length()-1));
                }
                //牵头单位
                if(StringUtils.isNotBlank(taskLeaderOrg)){
                    dcSupTaskInfo.setTaskLeaderOrg(taskLeaderOrg.toString().substring(0,taskLeaderOrg.toString().length()-1));
                }
                //牵头单位名称
                if(StringUtils.isNotBlank(taskLeaderOrgName)){
                    dcSupTaskInfo.setTaskLeaderOrgName(taskLeaderOrgName.toString().substring(0,taskLeaderOrgName.toString().length()-1));
                }
                //联系人
                dcSupTaskInfo.setContactName(otsDeployParent.getContacts());
                //联系电话
                dcSupTaskInfo.setContactPhone(otsDeployParent.getContactphone());

                //督办字号
                Integer  messageNumber  = dcSupTaskInfoService.getMaxMessageNumber();
                if(messageNumber==null){messageNumber=0;}
                messageNumber=messageNumber+1;
                dcSupTaskInfo.setMessageNumber("督一〔2022〕"+messageNumber+"");
                //反馈附件是否必填
                OtsDeploy otsDeploy=new OtsDeploy();
                otsDeploy.setTaskid(otsDeployMap.getTaskid());
                otsDeploy=oldDataDao.findOtsDeploy(otsDeploy);
                if(otsDeploy.getFeedannex()==0){
                    dcSupTaskInfo.setAttachmentFlag("1");
                }else {
                    dcSupTaskInfo.setAttachmentFlag("0");
                }
                //
                if(otsDeploy.getApproveuserid()!=null){
                    dcSupTaskInfo.setAuditorCode(String.valueOf(otsDeploy.getApproveuserid()));
                }
                dcSupTaskInfo.setAuditorName(otsDeploy.getApproveusername());
                //反馈时间要求
                OtsTimeNode otsTimeNode=new OtsTimeNode();
                otsTimeNode.setTaskid(otsDeploy.getTaskid());
                List<OtsTimeNode> otsTimeNodeList=oldDataDao.findListOtsTimeNode(otsTimeNode);
                List<DcSupTaskInfoAdd> dcSupTaskInfoAddList=new ArrayList<>();
                for (OtsTimeNode otn:otsTimeNodeList) {
                    DcSupTaskInfoAdd dcSupTaskInfoAdd=new DcSupTaskInfoAdd();
                    dcSupTaskInfoAdd.setTaskId(String.valueOf(otsDeployParent.getTaskid()));
                    dcSupTaskInfoAdd.setFeedbackStartTime(otn.getFixdate());
                    dcSupTaskInfoAdd.setFeedbackEndTime(otn.getFixdate());
                    dcSupTaskInfoAddList.add(dcSupTaskInfoAdd);
                }
                dcSupTaskInfo.setDcSupTaskInfoAddList(dcSupTaskInfoAddList);
                dcSupTaskInfoService.batchSaveTaskAddTime(dcSupTaskInfo);
                //附件
                if(otsDeployParent.getStatus()==1){
                    dcSupTaskInfo.setTaskStatus("1");
                }else {
                    dcSupTaskInfo.setTaskStatus("8");
                }
                dcSupTaskInfo.setNormalFlag("1");
                dcSupTaskInfoDao.insert(dcSupTaskInfo);
                count++;
            }
            System.out.println("总共更新" + count + "条数据");
        } catch (Exception e) {
            e.printStackTrace();
            code="error";
        }finally {
            return code;
        }
    }
    @Transactional(rollbackFor = Exception.class)
    public String findlistTaskAction() {
        String code="ok";
        int count=0;
        try {
            List<OtsDeploy> otsDeployList = oldDataDao.findlistTaskAction();
            for (OtsDeploy otsDeploy:otsDeployList) {
                DcSupTaskAction dcSupTaskAction = new DcSupTaskAction();
                //主键id
                dcSupTaskAction.setId(String.valueOf(otsDeploy.getTaskid()));
                //任务主键
                dcSupTaskAction.setTaskId(String.valueOf(otsDeploy.getFataskid()));
                //牵头单位`牵头单位名称
                OtsSubject otsSubject=new OtsSubject();
                otsSubject.setTaskid(otsDeploy.getTaskid());
                otsSubject.setFlag(0);
                List<OtsSubject> otsSubjectList=oldDataDao.findListOtsSubject(otsSubject);
                dcSupTaskAction.setLeaderOrgCode(String.valueOf(otsSubjectList.get(0).getSubjectid()));
                dcSupTaskAction.setLeaderOrgName(otsSubjectList.get(0).getSubjectname());
                //是否转发
                dcSupTaskAction.setTransferFlag("0");
                //是否批示
                dcSupTaskAction.setExplainFlag("0");
                //状态
                if(otsDeploy.getStatus()==1){
                     continue;
                }
                if(otsDeploy.getStatus()==16||otsDeploy.getStatus()==17||otsDeploy.getStatus()==18){
                     dcSupTaskAction.setTaskStatus("2");
                }else if (otsDeploy.getStatus()==20||otsDeploy.getStatus()==30||otsDeploy.getStatus()==40){
                    dcSupTaskAction.setTaskStatus("3");
                }else {
                    dcSupTaskAction.setTaskStatus("4");
                }
                if(otsDeploy.getProgress()==1){
                    dcSupTaskAction.setTaskProgress("3");
                }else if (otsDeploy.getProgress()==2){
                    dcSupTaskAction.setTaskProgress("4");
                }else if (otsDeploy.getProgress()==3){
                    dcSupTaskAction.setTaskProgress("1");
                }else if (otsDeploy.getProgress()==0){
                    dcSupTaskAction.setTaskProgress("2");
                }
                dcSupTaskAction.setCreateBy("system");
                dcSupTaskAction.setCreateDate(new Date());

                dcSupTaskActionDao.insert(dcSupTaskAction);

                //反馈时间要求
                DcSupTaskInfo dcSupTaskInfo=new DcSupTaskInfo();
                OtsTimeNode otsTimeNode=new OtsTimeNode();
                otsTimeNode.setTaskid(otsDeploy.getTaskid());
                List<OtsTimeNode> otsTimeNodeList=oldDataDao.findListOtsTimeNode(otsTimeNode);
                List<DcSupTaskInfoAdd> dcSupTaskInfoAddList=new ArrayList<>();
                for (OtsTimeNode otn:otsTimeNodeList) {
                    DcSupTaskInfoAdd dcSupTaskInfoAdd=new DcSupTaskInfoAdd();
                    dcSupTaskInfoAdd.setTaskId(String.valueOf(otsDeploy.getFataskid()));
                    dcSupTaskInfoAdd.setFeedbackStartTime(otn.getFixdate());
                    dcSupTaskInfoAdd.setFeedbackEndTime(otn.getFixdate());
                    dcSupTaskInfoAddList.add(dcSupTaskInfoAdd);
                }
                dcSupTaskInfo.setId(String.valueOf(otsDeploy.getFataskid()));
                dcSupTaskInfo.setDcSupTaskInfoAddList(dcSupTaskInfoAddList);
                //为每个分发牵头责任单位插入反馈时间段到反馈表
                for (DcSupTaskInfoAdd dcSupTaskInfoAdd : dcSupTaskInfo.getDcSupTaskInfoAddList()) {
                    if (dcSupTaskInfoAdd.getFeedbackStartTime() != null && dcSupTaskInfoAdd.getFeedbackEndTime() != null) {
                        DcSupTaskFeedback feedback = new DcSupTaskFeedback();
                        feedback.setTaskId(dcSupTaskInfo.getId());
                        feedback.setActionId(dcSupTaskAction.getId());
                        feedback.setFeedbackEndTime(dcSupTaskInfoAdd.getFeedbackEndTime());
                        feedback.setFeedbackStartTime(dcSupTaskInfoAdd.getFeedbackStartTime());
                        feedback.setFeedbackStatus("-1");
                        feedback.setFeedbackRequire(dcSupTaskInfoAdd.getFeedbackRequire());
                        dcSupTaskFeedbackDao.insert(feedback);
                    }

                }
                count++;
            }
            System.out.println("总共更新" + count + "条数据");
        } catch (Exception e) {
            e.printStackTrace();
            code="error";
        } finally {
            return code;
        }
    }



    public List<Map<Object, Object>> findTaskFile() {
        return oldDataDao.findTaskFile();
    }

    public List<Map<Object, Object>> findActionFile() {
        return oldDataDao.findActionFile();
    }
    @Transactional(rollbackFor = Exception.class)
    public String findlistTaskFeedback() {
        try {
            List<OtsDeployMap> otsDeployMapList=oldDataDao.findListOtsDeploy();
            for (OtsDeployMap otsDeployMap:otsDeployMapList) {
                OtsFeedbackInfo otsFeedbackInfo=new OtsFeedbackInfo();
                otsFeedbackInfo.setTaskid(otsDeployMap.getTaskid());
                List<OtsFeedbackInfo> otsFeedbackInfoList = oldDataDao.findlistfeedbackInfo(otsFeedbackInfo);
                DcSupTaskFeedback dcSupTaskFeedback=new DcSupTaskFeedback();
                dcSupTaskFeedback.setActionId(String.valueOf(otsDeployMap.getTaskid()));
                List<DcSupTaskFeedback> dcSupTaskFeedbackList=dcSupTaskFeedbackService.findList(dcSupTaskFeedback);
                for (int i = 0; i <otsFeedbackInfoList.size(); i++) {
                    DcSupTaskFeedback dk=new DcSupTaskFeedback();
                    dk.setId(dcSupTaskFeedbackList.get(i).getId());
                    dk.setFeedbackContent(otsFeedbackInfoList.get(i).getRemark());
                    dk.setFeedbackAdvice(otsFeedbackInfoList.get(i).getRsldesc());
                    dk.setFeedbackTime(otsFeedbackInfoList.get(i).getStarttime());
                    dk.setFeedbackName(otsFeedbackInfoList.get(i).getContacts());
                    dk.setFeedbackPhone(otsFeedbackInfoList.get(i).getContactPhone());
                    if(otsFeedbackInfoList.get(i).getProgress()==1){
                        dk.setFeedbackProgress("1");
                    }else if (otsFeedbackInfoList.get(i).getProgress()==2){
                        dk.setFeedbackProgress("3");
                    }else if (otsFeedbackInfoList.get(i).getProgress()==3){
                        dk.setFeedbackProgress("2");
                    }else {
                        dk.setFeedbackProgress("4");
                    }
                    if(otsFeedbackInfoList.get(i).getStatus()==2){
                        dk.setFeedbackStatus("2");
                    }else if(otsFeedbackInfoList.get(i).getStatus()==3){
                        dk.setFeedbackStatus("9");
                    }else if(otsFeedbackInfoList.get(i).getStatus()==4){
                        dk.setFeedbackStatus("10");
                    }else{
                        dk.setFeedbackStatus("2");
                    }
                    if(otsFeedbackInfoList.get(i).getIsstandard()==1){
                        dk.setAuditFlag("1");
                        dk.setAuditorCode(String.valueOf(otsFeedbackInfoList.get(i).getUserid()));
                        dk.setAuditorName(otsFeedbackInfoList.get(i).getUsername());
                    }else {
                        dk.setAuditFlag("0");
                    }
                    if("100".equals(otsFeedbackInfoList.get(i).getRemarks5())){
                        dk.setFeedbackRate(100d);
                    }
                    dcSupTaskFeedbackDao.update(dk);
                    dk.setRemarks2(String.valueOf(otsFeedbackInfoList.get(i).getId()));
                    dcSupTaskFeedbackDao.updateId(dk);

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return "ok";
        }

    }

    public List<Map<Object, Object>> findFeedbackFile(String ss) {
        return oldDataDao.findFeedbackFile(ss);
    }

    @Transactional(rollbackFor = Exception.class)
    public void findTaskAudit() {
        try {
            List<DcSupTaskFeedback> dcSupTaskFeedbackList=dcSupTaskFeedbackService.findList(new DcSupTaskFeedback());
            for (DcSupTaskFeedback dcSupTaskFeedback:dcSupTaskFeedbackList) {
                OtsAudit otsAudit=new OtsAudit();
                otsAudit.setFeedbackid(Long.valueOf(dcSupTaskFeedback.getId()));
               List<OtsAudit> otsAuditList=oldDataDao.findTaskAudit(otsAudit);
               if(otsAuditList.size()>0){
                   for (OtsAudit oa:otsAuditList) {
                       DcSupTaskAudit dcSupTaskAudit=new DcSupTaskAudit();
                       dcSupTaskAudit.setTargetId(String.valueOf(oa.getFeedbackid()));
                       dcSupTaskAudit.setTargetType("2");
                       dcSupTaskAudit.setAuditType("3");
                       dcSupTaskAudit.setAuditorCode(String.valueOf(oa.getSubjectid()));
                       dcSupTaskAudit.setAuditorName(oa.getSubject());
                       dcSupTaskAudit.setAuditorRole("NORMAL");
                       if(oa.getStatus()==3){
                           dcSupTaskAudit.setAuditResult("1");
                           dcSupTaskAudit.setAuditTime(oa.getOpttime());
                           dcSupTaskAudit.setAuditStatus("2");
                       }
                       if(oa.getStatus()==4){
                           dcSupTaskAudit.setAuditResult("2");
                           dcSupTaskAudit.setAuditTime(oa.getOpttime());
                           dcSupTaskAudit.setAuditStatus("2");
                       }
                       if(oa.getStatus()==1){
                           dcSupTaskAudit.setAuditStatus("1");
                       }
                       dcSupTaskAudit.setAuditOption(oa.getSuggestion());
                       dcSupTaskAudit.setTaskId(dcSupTaskFeedback.getTaskId());
                       dcSupTaskAuditDao.insert(dcSupTaskAudit);
                   }

               }
               List<OtsAudit> otsAudits=oldDataDao.findTaskAuditByid(otsAudit);
               if(otsAudits.size()>0){
                   for (OtsAudit oa:otsAudits) {
                       DcSupTaskAudit dcSupTaskAudit=new DcSupTaskAudit();
                       dcSupTaskAudit.setTargetId(String.valueOf(oa.getFeedbackid()));
                       dcSupTaskAudit.setTargetType("2");
                       dcSupTaskAudit.setAuditType("3");
                       dcSupTaskAudit.setAuditorCode(String.valueOf(oa.getSubjectid()));
                       dcSupTaskAudit.setAuditorName(oa.getSubject());
                       dcSupTaskAudit.setAuditorRole("NORMAL");
                       if(oa.getStatus()==3){
                           dcSupTaskAudit.setAuditResult("1");
                           dcSupTaskAudit.setAuditTime(oa.getOpttime());
                           dcSupTaskAudit.setAuditStatus("2");
                       }
                       if(oa.getStatus()==4){
                           dcSupTaskAudit.setAuditResult("2");
                           dcSupTaskAudit.setAuditTime(oa.getOpttime());
                           dcSupTaskAudit.setAuditStatus("2");
                       }
                       if(oa.getStatus()==1){
                           dcSupTaskAudit.setAuditStatus("1");
                       }
                       dcSupTaskAudit.setAuditOption(oa.getSuggestion());
                       dcSupTaskAudit.setTaskId(dcSupTaskFeedback.getTaskId());
                       dcSupTaskAuditDao.insert(dcSupTaskAudit);
                   }
               }
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }
    @Transactional(rollbackFor = Exception.class)
    public void updateInfoCreateby() {
        try {
            List<DcSupTaskInfo> dcSupTaskInfoList=dcSupTaskInfoDao.findList(new DcSupTaskInfo());
            for (DcSupTaskInfo dcSupTaskInfo:dcSupTaskInfoList) {
                OtsDeployParent otsDeployParent=oldDataDao.findOtsDeployParent(dcSupTaskInfo.getId());
                DcSupTaskInfo dt=new DcSupTaskInfo();
                dt.setId(dcSupTaskInfo.getId());
                dt.setCreateBy(String.valueOf(otsDeployParent.getUserid()));
                dt.setCreateByName(otsDeployParent.getUsername());
                dt.setUpdateBy(String.valueOf(otsDeployParent.getUserid()));
                dt.setUpdateByName(otsDeployParent.getUsername());
                dt.setCreateDate(otsDeployParent.getCreatetime());
                dt.setUpdateDate(otsDeployParent.getUpdatetime());
                dt.setCreateOrg(String.valueOf(otsDeployParent.getUserorgid()));
                dt.setCreateOrgName(otsDeployParent.getUserorgname());
                String taskLeaderOrgName=dcSupTaskActionDao.finTaskLeaderOrgName(dcSupTaskInfo.getId());
                dt.setTaskLeaderOrgName(taskLeaderOrgName);
                dcSupTaskInfoDao.update(dt);
                dcSupTaskActionDao.updateTask(dt);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Transactional(rollbackFor = Exception.class)
    public void updateFeedbackCreateby() {
        try {
            List<DcSupTaskFeedback> dcSupTaskFeedbackList=dcSupTaskFeedbackDao.findList(new DcSupTaskFeedback());
            for (DcSupTaskFeedback dcSupTaskFeedback:dcSupTaskFeedbackList) {
                if(dcSupTaskFeedback.getId().length()>10){
                    continue;
                }
                OtsFeedbackInfo otsFeedbackInfo=oldDataDao.findOtsFeedBack(dcSupTaskFeedback.getId());
                DcSupTaskFeedback dt=new DcSupTaskFeedback();
                dt.setUpdateBy(String.valueOf(otsFeedbackInfo.getObjid()));
                dt.setUpdateByName(otsFeedbackInfo.getObjname());
                dt.setUpdateDate(otsFeedbackInfo.getStarttime());
                dt.setId(dcSupTaskFeedback.getId());
                dcSupTaskActionDao.updateFeedBack(dt);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Transactional(rollbackFor = Exception.class)
    public void updateInfoFeedback() {
        try {
            List<DcSupTaskInfo> dcSupTaskInfoList=dcSupTaskInfoDao.findList(new DcSupTaskInfo());
            for (DcSupTaskInfo dcSupTaskInfo:dcSupTaskInfoList) {
                OtsDeployParent otsDeployParent=oldDataDao.findOtsDeployParent(dcSupTaskInfo.getId());
                DcSupTaskInfo dt=new DcSupTaskInfo();
                dt.setId(dcSupTaskInfo.getId());
                if(otsDeployParent.getNeedbacktime()==1){
                    dt.setFeedbackWay("2");
                }else if (otsDeployParent.getNeedbacktime()==2){
                    dt.setFeedbackWay("1");
                }else if(otsDeployParent.getNeedbacktime()==3){
                    dt.setFeedbackWay("4");
                }else if(otsDeployParent.getNeedbacktime()==4){
                    dt.setFeedbackWay("3");
                }else {
                    dt.setFeedbackWay("4");
                }
                if(otsDeployParent.getBacktime()!=0){
                    dt.setFeedbackRate(String.valueOf(otsDeployParent.getBacktime()));
                }
                dt.setCreateUnit(String.valueOf(otsDeployParent.getUserorgid()));
                dt.setCreateUnitName(otsDeployParent.getUserorgname());
                dcSupTaskInfoDao.update(dt);
                dt.setUpdateBy(String.valueOf(otsDeployParent.getUserid()));
                dt.setUpdateDate(otsDeployParent.getUpdatetime());
                dcSupTaskActionDao.updateTask(dt);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<Map<Object, Object>> findCompleteTaskFile() {
        return oldDataDao.findCompleteTaskFile();
    }

    public List<Map<Object, Object>> findCompleteFeedbackFile() {
        return oldDataDao.findCompleteFeedbackFile();
    }
    @Transactional(rollbackFor = Exception.class)
    public String findBeforelistTask() {
        String code="ok";
        int count=0;
        try {
            List<OtsDeployParent> otsDeployParentList = oldDataDao.findBeforelistTask();
            for (OtsDeployParent otsDeployParent:otsDeployParentList) {
                DcSupTaskInfo dcSupTaskInfo=new DcSupTaskInfo();
                dcSupTaskInfo.setId(String.valueOf(otsDeployParent.getTaskid()));
                //任务年度
                dcSupTaskInfo.setTaskYear(String.valueOf(otsDeployParent.getYears()));
                //任务类型主键
                dcSupTaskInfo.setTaskTypeCode(String.valueOf(otsDeployParent.getSourceid()));
                //任务类型名称
                dcSupTaskInfo.setTaskTypeName(otsDeployParent.getSourceName());
                //任务标题
                dcSupTaskInfo.setTaskTitle(otsDeployParent.getTitle());
                //任务内容
                dcSupTaskInfo.setTaskContent(otsDeployParent.getTkname());
                //任务开始时间
                dcSupTaskInfo.setTaskStartTime(otsDeployParent.getStarttime());
                //任务结束时间
                dcSupTaskInfo.setTaskEndTime(otsDeployParent.getEndtime());
                //牵头领导
                //牵头单位
                OtsDeployMap otsDeployMap=new OtsDeployMap();
                otsDeployMap.setPtaskid(otsDeployParent.getTaskid());
                otsDeployMap=oldDataDao.findotsDeployMap(otsDeployMap);
                OtsSubject otsSubject=new OtsSubject();
                otsSubject.setTaskid(otsDeployMap.getTaskid());
                List<OtsSubject> otsSubjectList=oldDataDao.findListOtsSubject(otsSubject);
                StringBuffer taskLeader=new StringBuffer();
                StringBuffer taskLeaderName=new StringBuffer();
                StringBuffer taskLeaderOrg=new StringBuffer();
                StringBuffer taskLeaderOrgName=new StringBuffer();
                for (OtsSubject osj:otsSubjectList) {
                    //牵头领导主键
                    if(osj.getFlag()==3){
                        taskLeader.append(osj.getSubjectid());
                        taskLeader.append(",");
                    }
                    //牵头领导名称
                    if(osj.getFlag()==3){
                        taskLeaderName.append(osj.getSubjectname());
                        taskLeaderName.append(",");
                    }
                    //牵头单位主键
                    if(osj.getFlag()==0){
                        taskLeaderOrg.append(osj.getSubjectid());
                        taskLeaderOrg.append(",");
                    }
                    //牵头单位名称
                    if(osj.getFlag()==0){
                        taskLeaderOrgName.append(osj.getSubjectname());
                        taskLeaderOrgName.append(",");
                    }
                }
                //牵头领导
                if(StringUtils.isNotBlank(taskLeader)){
                    dcSupTaskInfo.setTaskLeader(taskLeader.toString().substring(0,taskLeader.toString().length()-1));
                }
                //牵头领导名称
                if(StringUtils.isNotBlank(taskLeaderName)){
                    dcSupTaskInfo.setTaskLeaderName(taskLeaderName.toString().substring(0,taskLeaderName.toString().length()-1));
                }
                //牵头单位
                if(StringUtils.isNotBlank(taskLeaderOrg)){
                    dcSupTaskInfo.setTaskLeaderOrg(taskLeaderOrg.toString().substring(0,taskLeaderOrg.toString().length()-1));
                }
                //牵头单位名称
                if(StringUtils.isNotBlank(taskLeaderOrgName)){
                    dcSupTaskInfo.setTaskLeaderOrgName(taskLeaderOrgName.toString().substring(0,taskLeaderOrgName.toString().length()-1));
                }
                //联系人
                dcSupTaskInfo.setContactName(otsDeployParent.getContacts());
                //联系电话
                dcSupTaskInfo.setContactPhone(otsDeployParent.getContactphone());

                //督办字号
//                Integer  messageNumber  = dcSupTaskInfoService.getMaxMessageNumber();
//                if(messageNumber==null){messageNumber=0;}
//                messageNumber=messageNumber+1;
//                dcSupTaskInfo.setMessageNumber("督一〔2022〕"+messageNumber+"");
                //反馈附件是否必填
                OtsDeploy otsDeploy=new OtsDeploy();
                otsDeploy.setTaskid(otsDeployMap.getTaskid());
                otsDeploy=oldDataDao.findOtsDeploy(otsDeploy);
                if(otsDeploy.getFeedannex()==0){
                    dcSupTaskInfo.setAttachmentFlag("1");
                }else {
                    dcSupTaskInfo.setAttachmentFlag("0");
                }
                //
                if(otsDeploy.getApproveuserid()!=null){
                    dcSupTaskInfo.setAuditorCode(String.valueOf(otsDeploy.getApproveuserid()));
                }
                dcSupTaskInfo.setAuditorName(otsDeploy.getApproveusername());
                //反馈时间要求
                OtsTimeNode otsTimeNode=new OtsTimeNode();
                otsTimeNode.setTaskid(otsDeploy.getTaskid());
                List<OtsTimeNode> otsTimeNodeList=oldDataDao.findListOtsTimeNode(otsTimeNode);
                List<DcSupTaskInfoAdd> dcSupTaskInfoAddList=new ArrayList<>();
                for (OtsTimeNode otn:otsTimeNodeList) {
                    DcSupTaskInfoAdd dcSupTaskInfoAdd=new DcSupTaskInfoAdd();
                    dcSupTaskInfoAdd.setTaskId(String.valueOf(otsDeployParent.getTaskid()));
                    dcSupTaskInfoAdd.setFeedbackStartTime(otn.getFixdate());
                    dcSupTaskInfoAdd.setFeedbackEndTime(otn.getFixdate());
                    dcSupTaskInfoAddList.add(dcSupTaskInfoAdd);
                }
                dcSupTaskInfo.setDcSupTaskInfoAddList(dcSupTaskInfoAddList);
                dcSupTaskInfoService.batchSaveTaskAddTime(dcSupTaskInfo);
                //附件
                if(otsDeployParent.getStatus()==1){
                    dcSupTaskInfo.setTaskStatus("1");
                }else {
                    dcSupTaskInfo.setTaskStatus("8");
                }
                dcSupTaskInfo.setNormalFlag("1");
                dcSupTaskInfoDao.insert(dcSupTaskInfo);
                count++;
            }
            System.out.println("总共更新" + count + "条数据");
        } catch (Exception e) {
            e.printStackTrace();
            code="error";
        }finally {
            return code;
        }
    }
    @Transactional(rollbackFor = Exception.class)
    public String findBeforelistTaskAction() {
        String code="ok";
        int count=0;
        try {
            List<OtsDeploy> otsDeployList = oldDataDao.findBeforelistTaskAction();
            for (OtsDeploy otsDeploy:otsDeployList) {
                DcSupTaskAction dcSupTaskAction = new DcSupTaskAction();
                //主键id
                dcSupTaskAction.setId(String.valueOf(otsDeploy.getTaskid()));
                //任务主键
                dcSupTaskAction.setTaskId(String.valueOf(otsDeploy.getFataskid()));
                //牵头单位`牵头单位名称
                OtsSubject otsSubject=new OtsSubject();
                otsSubject.setTaskid(otsDeploy.getTaskid());
                otsSubject.setFlag(0);
                List<OtsSubject> otsSubjectList=oldDataDao.findListOtsSubject(otsSubject);
                dcSupTaskAction.setLeaderOrgCode(String.valueOf(otsSubjectList.get(0).getSubjectid()));
                dcSupTaskAction.setLeaderOrgName(otsSubjectList.get(0).getSubjectname());
                //是否转发
                dcSupTaskAction.setTransferFlag("0");
                //是否批示
                dcSupTaskAction.setExplainFlag("0");
                //状态
                if(otsDeploy.getStatus()==1){
                    continue;
                }
                if(otsDeploy.getStatus()==16||otsDeploy.getStatus()==17||otsDeploy.getStatus()==18){
                    dcSupTaskAction.setTaskStatus("2");
                }else if (otsDeploy.getStatus()==20||otsDeploy.getStatus()==30||otsDeploy.getStatus()==40){
                    dcSupTaskAction.setTaskStatus("3");
                }else {
                    dcSupTaskAction.setTaskStatus("4");
                }
                if(otsDeploy.getProgress()==1){
                    dcSupTaskAction.setTaskProgress("3");
                }else if (otsDeploy.getProgress()==2){
                    dcSupTaskAction.setTaskProgress("4");
                }else if (otsDeploy.getProgress()==3){
                    dcSupTaskAction.setTaskProgress("1");
                }else if (otsDeploy.getProgress()==0){
                    dcSupTaskAction.setTaskProgress("2");
                }
                dcSupTaskAction.setCreateBy("system");
                dcSupTaskAction.setCreateDate(new Date());

                dcSupTaskActionDao.insert(dcSupTaskAction);

                //反馈时间要求
                DcSupTaskInfo dcSupTaskInfo=new DcSupTaskInfo();
                OtsTimeNode otsTimeNode=new OtsTimeNode();
                otsTimeNode.setTaskid(otsDeploy.getTaskid());
                List<OtsTimeNode> otsTimeNodeList=oldDataDao.findListOtsTimeNode(otsTimeNode);
                List<DcSupTaskInfoAdd> dcSupTaskInfoAddList=new ArrayList<>();
                for (OtsTimeNode otn:otsTimeNodeList) {
                    DcSupTaskInfoAdd dcSupTaskInfoAdd=new DcSupTaskInfoAdd();
                    dcSupTaskInfoAdd.setTaskId(String.valueOf(otsDeploy.getFataskid()));
                    dcSupTaskInfoAdd.setFeedbackStartTime(otn.getFixdate());
                    dcSupTaskInfoAdd.setFeedbackEndTime(otn.getFixdate());
                    dcSupTaskInfoAddList.add(dcSupTaskInfoAdd);
                }
                dcSupTaskInfo.setId(String.valueOf(otsDeploy.getFataskid()));
                dcSupTaskInfo.setDcSupTaskInfoAddList(dcSupTaskInfoAddList);
                //为每个分发牵头责任单位插入反馈时间段到反馈表
                for (DcSupTaskInfoAdd dcSupTaskInfoAdd : dcSupTaskInfo.getDcSupTaskInfoAddList()) {
                    if (dcSupTaskInfoAdd.getFeedbackStartTime() != null && dcSupTaskInfoAdd.getFeedbackEndTime() != null) {
                        DcSupTaskFeedback feedback = new DcSupTaskFeedback();
                        feedback.setTaskId(dcSupTaskInfo.getId());
                        feedback.setActionId(dcSupTaskAction.getId());
                        feedback.setFeedbackEndTime(dcSupTaskInfoAdd.getFeedbackEndTime());
                        feedback.setFeedbackStartTime(dcSupTaskInfoAdd.getFeedbackStartTime());
                        feedback.setFeedbackStatus("-1");
                        feedback.setFeedbackRequire(dcSupTaskInfoAdd.getFeedbackRequire());
                        dcSupTaskFeedbackDao.insert(feedback);
                    }

                }
                count++;
            }
            System.out.println("总共更新" + count + "条数据");
        } catch (Exception e) {
            e.printStackTrace();
            code="error";
        } finally {
            return code;
        }
    }
    @Transactional(rollbackFor = Exception.class)
    public String findBeforelistTaskFeedback() {
        try {
            List<OtsDeployMap> otsDeployMapList=oldDataDao.findBeforeListOtsDeploy();
            for (OtsDeployMap otsDeployMap:otsDeployMapList) {
                OtsFeedbackInfo otsFeedbackInfo=new OtsFeedbackInfo();
                otsFeedbackInfo.setTaskid(otsDeployMap.getTaskid());
                List<OtsFeedbackInfo> otsFeedbackInfoList = oldDataDao.findlistfeedbackInfo(otsFeedbackInfo);
                DcSupTaskFeedback dcSupTaskFeedback=new DcSupTaskFeedback();
                dcSupTaskFeedback.setActionId(String.valueOf(otsDeployMap.getTaskid()));
                List<DcSupTaskFeedback> dcSupTaskFeedbackList=dcSupTaskFeedbackService.findList(dcSupTaskFeedback);
                for (int i = 0; i <otsFeedbackInfoList.size(); i++) {
                    DcSupTaskFeedback dk=new DcSupTaskFeedback();
                    dk.setId(dcSupTaskFeedbackList.get(i).getId());
                    dk.setFeedbackContent(otsFeedbackInfoList.get(i).getRemark());
                    dk.setFeedbackAdvice(otsFeedbackInfoList.get(i).getRsldesc());
                    dk.setFeedbackTime(otsFeedbackInfoList.get(i).getStarttime());
                    dk.setFeedbackName(otsFeedbackInfoList.get(i).getContacts());
                    dk.setFeedbackPhone(otsFeedbackInfoList.get(i).getContactPhone());
                    if(otsFeedbackInfoList.get(i).getProgress()==1){
                        dk.setFeedbackProgress("1");
                    }else if (otsFeedbackInfoList.get(i).getProgress()==2){
                        dk.setFeedbackProgress("3");
                    }else if (otsFeedbackInfoList.get(i).getProgress()==3){
                        dk.setFeedbackProgress("2");
                    }else {
                        dk.setFeedbackProgress("4");
                    }
                    if(otsFeedbackInfoList.get(i).getStatus()==2){
                        dk.setFeedbackStatus("2");
                    }else if(otsFeedbackInfoList.get(i).getStatus()==3){
                        dk.setFeedbackStatus("9");
                    }else if(otsFeedbackInfoList.get(i).getStatus()==4){
                        dk.setFeedbackStatus("10");
                    }else{
                        dk.setFeedbackStatus("2");
                    }
                    if(otsFeedbackInfoList.get(i).getIsstandard()==1){
                        dk.setAuditFlag("1");
                        dk.setAuditorCode(String.valueOf(otsFeedbackInfoList.get(i).getUserid()));
                        dk.setAuditorName(otsFeedbackInfoList.get(i).getUsername());
                    }else {
                        dk.setAuditFlag("0");
                    }
                    if("100".equals(otsFeedbackInfoList.get(i).getRemarks5())){
                        dk.setFeedbackRate(100d);
                    }
                    dcSupTaskFeedbackDao.update(dk);
                    dk.setRemarks2(String.valueOf(otsFeedbackInfoList.get(i).getId()));
                    dcSupTaskFeedbackDao.updateId(dk);

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return "ok";
        }

    }
    @Transactional(rollbackFor = Exception.class)
    public void findBeforeTaskAudit() {
        try {
            List<DcSupTaskFeedback> dcSupTaskFeedbackList=dcSupTaskFeedbackService.findList(new DcSupTaskFeedback());
            for (DcSupTaskFeedback dcSupTaskFeedback:dcSupTaskFeedbackList) {
                OtsAudit otsAudit=new OtsAudit();
                otsAudit.setFeedbackid(Long.valueOf(dcSupTaskFeedback.getId()));
                List<OtsAudit> otsAuditList=oldDataDao.findTaskAudit(otsAudit);
                if(otsAuditList.size()>0){
                    for (OtsAudit oa:otsAuditList) {
                        DcSupTaskAudit dcSupTaskAudit=new DcSupTaskAudit();
                        dcSupTaskAudit.setTargetId(String.valueOf(oa.getFeedbackid()));
                        dcSupTaskAudit.setTargetType("2");
                        dcSupTaskAudit.setAuditType("3");
                        dcSupTaskAudit.setAuditorCode(String.valueOf(oa.getSubjectid()));
                        dcSupTaskAudit.setAuditorName(oa.getSubject());
                        dcSupTaskAudit.setAuditorRole("NORMAL");
                        if(oa.getStatus()==3){
                            dcSupTaskAudit.setAuditResult("1");
                            dcSupTaskAudit.setAuditTime(oa.getOpttime());
                            dcSupTaskAudit.setAuditStatus("2");
                        }
                        if(oa.getStatus()==4){
                            dcSupTaskAudit.setAuditResult("2");
                            dcSupTaskAudit.setAuditTime(oa.getOpttime());
                            dcSupTaskAudit.setAuditStatus("2");
                        }
                        if(oa.getStatus()==1){
                            dcSupTaskAudit.setAuditStatus("1");
                        }
                        dcSupTaskAudit.setAuditOption(oa.getSuggestion());
                        dcSupTaskAudit.setTaskId(dcSupTaskFeedback.getTaskId());
                        dcSupTaskAuditDao.insert(dcSupTaskAudit);
                    }

                }
                List<OtsAudit> otsAudits=oldDataDao.findTaskAuditByid(otsAudit);
                if(otsAudits.size()>0){
                    for (OtsAudit oa:otsAudits) {
                        DcSupTaskAudit dcSupTaskAudit=new DcSupTaskAudit();
                        dcSupTaskAudit.setTargetId(String.valueOf(oa.getFeedbackid()));
                        dcSupTaskAudit.setTargetType("2");
                        dcSupTaskAudit.setAuditType("3");
                        dcSupTaskAudit.setAuditorCode(String.valueOf(oa.getSubjectid()));
                        dcSupTaskAudit.setAuditorName(oa.getSubject());
                        dcSupTaskAudit.setAuditorRole("NORMAL");
                        if(oa.getStatus()==3){
                            dcSupTaskAudit.setAuditResult("1");
                            dcSupTaskAudit.setAuditTime(oa.getOpttime());
                            dcSupTaskAudit.setAuditStatus("2");
                        }
                        if(oa.getStatus()==4){
                            dcSupTaskAudit.setAuditResult("2");
                            dcSupTaskAudit.setAuditTime(oa.getOpttime());
                            dcSupTaskAudit.setAuditStatus("2");
                        }
                        if(oa.getStatus()==1){
                            dcSupTaskAudit.setAuditStatus("1");
                        }
                        dcSupTaskAudit.setAuditOption(oa.getSuggestion());
                        dcSupTaskAudit.setTaskId(dcSupTaskFeedback.getTaskId());
                        dcSupTaskAuditDao.insert(dcSupTaskAudit);
                    }
                }
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }

    public List<Map<Object, Object>> findBeforeTaskFile() {
        return oldDataDao.findBeforeTaskFile();
    }

    public List<Map<Object, Object>> findBeforeFeedbackFile() {
        return oldDataDao.findBeforeFeedbackFile();
    }
}
